﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
 
/// <summary>
/// Class for Tournament object
/// </summary>
public class Tournament
{
    public enum tournamentType { 
        DrawBased = 0,
        FreePlay = 1
    }


    private string _Tournament_Name;
    private string _Tournament_Logo;
    private int _Pk_Tournament;
    private int _QualifyRounds;
    private int _QualifyPlayersToPlayOffs;
    private int _PlayOffsRounds;
    private int _TournamentType;

    public int TournamentType
    {
        get { return _TournamentType; }
        set { _TournamentType = value; }
    }
     
    public string Tournament_Name
    {
        get { return _Tournament_Name; }
        set { _Tournament_Name = value; }
    }
    public string Tournament_Logo
    {
        get { return _Tournament_Logo; }
        set { _Tournament_Logo = value; }
    }
    public int Pk_Tournament
    {
        get { return _Pk_Tournament; }
        set { _Pk_Tournament = value; }
    }
    public int QualifyRounds
    {
        get { return _QualifyRounds; }
        set { _QualifyRounds = value; }
    }
    public int QualifyPlayersToPlayOffs
    {
        get { return _QualifyPlayersToPlayOffs; }
        set { _QualifyPlayersToPlayOffs = value; }
    }
    public int PlayOffsRounds
{
  get { return _PlayOffsRounds; }
  set { _PlayOffsRounds = value; }
}  

  	public Tournament()
	{
		
	}

    public Tournament(int p_Pk_Tournament)
    {

        DataTable oDataTable = DataBase.dt("SELECT Tournament_Name,Tournament_Logo,QualifyRounds,QualifyPlayersToPlayOffs,PlayOffsRounds,TournamentType FROM Tournament WHERE Pk_Tournament=" + p_Pk_Tournament.ToString());   

            if(oDataTable.Rows.Count >0){
                this._Pk_Tournament = p_Pk_Tournament;
                this._Tournament_Name = oDataTable.Rows[0]["Tournament_Name"].ToString();
                this._Tournament_Logo =  oDataTable.Rows[0]["Tournament_Logo"].ToString();
                this._QualifyRounds = Int32.Parse(oDataTable.Rows[0]["QualifyRounds"].ToString());
                this._QualifyPlayersToPlayOffs = Int32.Parse(oDataTable.Rows[0]["QualifyPlayersToPlayOffs"].ToString());
                this._PlayOffsRounds = Int32.Parse(oDataTable.Rows[0]["PlayOffsRounds"].ToString());
                if (oDataTable.Rows[0]["TournamentType"].ToString() == string.Empty)
                {
                    this._TournamentType = 0;
                }
                else {
                    this._TournamentType = Int32.Parse(oDataTable.Rows[0]["TournamentType"].ToString());
                }
                
            }                 

    }

    private Tournament(int p_Pk_Tournament, string p_Tournament_Name, string p_Tournament_Logo, int p_QualifyRounds, int p_QualifyPlayersToPlayOffs, int p_PlayOffsRounds, int p_TournamentType)
    {
        this._Pk_Tournament = p_Pk_Tournament;
        this._Tournament_Name = p_Tournament_Name;
        this._Tournament_Logo = p_Tournament_Logo;
        this._QualifyRounds = p_QualifyRounds;
        this._QualifyPlayersToPlayOffs = p_QualifyPlayersToPlayOffs;
        this._PlayOffsRounds = p_PlayOffsRounds;
        this._TournamentType = p_TournamentType; 
    }

    public static int CreateTournament(string p_Tournament_Name, string p_Tournament_Logo, int p_NumberOfQualifyRounds, int p_NumberOfPlayoffRounds,int p_NumberOfQualifyPlayersToPlayoffs,int p_TournamentType)
    {
        OleDbConnection objConnection = new OleDbConnection();
        try
        {
            objConnection = DataBase.GetConnection();
            OleDbCommand objCommand = new OleDbCommand("INSERT INTO Tournament (Tournament_Name,Tournament_Logo,QualifyRounds,QualifyPlayersToPlayOffs,PlayOffsRounds,TournamentType) VALUES ('" + p_Tournament_Name + "','" + p_Tournament_Logo + "'," + p_NumberOfQualifyRounds + "," + p_NumberOfQualifyPlayersToPlayoffs + "," + p_NumberOfPlayoffRounds + "," + p_TournamentType + ")", objConnection);
            objConnection.Open();
            objCommand.ExecuteScalar();

            OleDbCommand cmdGetIdentity = new OleDbCommand();
            cmdGetIdentity.CommandText = "SELECT @@IDENTITY";
            cmdGetIdentity.Connection = objConnection;
            return Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString());
                        
        }
        catch(Exception Ex)
        {

            throw;
        }
        finally {
            objConnection.Close(); 
        } 
    }

    public static Boolean DeleteTournament(int p_Pk_Tournament)
    {
        OleDbConnection objConnection = new OleDbConnection();
        try
        {
            objConnection = DataBase.GetConnection();
            OleDbCommand objCommand = new OleDbCommand("DELETE * FROM PlayOffsTournamentRounds WHERE Pk_Tournament=" + p_Pk_Tournament.ToString(), objConnection);
            objConnection.Open();
            objCommand.ExecuteNonQuery();

            objCommand = new OleDbCommand("DELETE * FROM QualifyTournamentRounds WHERE Pk_Tournament=" + p_Pk_Tournament.ToString(), objConnection);
            objCommand.ExecuteNonQuery();

            objCommand = new OleDbCommand("DELETE * FROM PlayerTournament WHERE Fk_Tournament=" + p_Pk_Tournament.ToString(), objConnection);
            objCommand.ExecuteNonQuery();

            objCommand = new OleDbCommand("DELETE * FROM MachinesTournament WHERE Fk_Tournament=" + p_Pk_Tournament.ToString(), objConnection);
            objCommand.ExecuteNonQuery();
            
            //Borro todos los jugadores que ya no tengan torneo asignado
            objCommand = new OleDbCommand("DELETE * FROM Players where Pk_Player not in(SELECT DISTINCT Fk_Player from PlayerTournament)", objConnection);
            objCommand.ExecuteNonQuery();

            //Borro todas las maquinas que ya no tengan torneo asignado
            objCommand = new OleDbCommand("DELETE * FROM Machines where Pk_Machine not in(SELECT DISTINCT Fk_Machine from MachinesTournament)", objConnection);
            objCommand.ExecuteNonQuery();

            objCommand = new OleDbCommand("DELETE * FROM Tournament WHERE Pk_Tournament=" + p_Pk_Tournament.ToString(), objConnection);
            objCommand.ExecuteNonQuery();

            return true;
            

        }
        catch (Exception Ex)
        {
            return false; 
        }
        finally
        {
            objConnection.Close();
        }
    }

    public static List<Tournament> getAvailableTournaments()
    {
        try
        {
            DataTable oDataTable = DataBase.dt("SELECT Pk_Tournament,Tournament_Name,Tournament_Logo,QualifyRounds,QualifyPlayersToPlayOffs,PlayOffsRounds,TournamentType FROM Tournament");

            List<Tournament> ReturnList = new List<Tournament>();

            foreach (DataRow iRow in oDataTable.Rows)
            {   int TournamentType;
            if (iRow["TournamentType"].ToString() == string.Empty)
            { 
                TournamentType=0;
                }else{
                    TournamentType=Int32.Parse(iRow["TournamentType"].ToString());
                }

                Tournament oTournament = new Tournament(Int32.Parse(iRow["Pk_Tournament"].ToString()), iRow["Tournament_Name"].ToString(), iRow["Tournament_Logo"].ToString(), Int32.Parse(iRow["QualifyRounds"].ToString()), Int32.Parse(iRow["QualifyPlayersToPlayOffs"].ToString()), Int32.Parse(iRow["PlayOffsRounds"].ToString()), TournamentType);
                ReturnList.Add(oTournament);
            }
            return ReturnList; 
        }
        catch (Exception Ex)
        {
            throw;
        }
     
    }
    
}

